package com.xbongbong.pay.platform.wechatpay.utils;

import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import com.xbongbong.pay.platform.wechatpay.config.WeChatPayMeta;
import com.xbongbong.pay.utils.RsaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
 *
 * @author 魏荣杰
 * @date 2021/1/22 20:19
 * @since
 * @version
 */
public class WeChatPayCertUtil {
    private static final Logger LOG = LoggerFactory.getLogger(WeChatPayCertUtil.class);

    public static WeChatPayMeta createMeta(String keyPath, String keyAlias, String keyPwd, String keyType) {
        ClassPathResource resource = new ClassPathResource(keyPath);
        WeChatPayMeta weChatPayMeta = new WeChatPayMeta();
        try {
            KeyStore keyStore = KeyStore.getInstance(keyType);
            keyStore.load(resource.getInputStream(), keyPwd.toCharArray());
            X509Certificate certificate = (X509Certificate) keyStore.getCertificate(keyAlias);
            String serialNumber = certificate.getSerialNumber().toString(16).toUpperCase();
            PublicKey publicKey = certificate.getPublicKey();
            PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, keyPwd.toCharArray());
            weChatPayMeta.setKeyPair(new KeyPair(publicKey, privateKey));
            weChatPayMeta.setSerialNumber(serialNumber);
            return weChatPayMeta;
        } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException e) {
            LOG.error("com.xbongbong.pay.platform.wechatpay.utils.WeChatPayCertUtil.createMeta failed ", e);
        }
        return weChatPayMeta;
    }




    public static void main(String[] args) throws Exception{
        String keyPwd = "1293729201";
        ClassPathResource resource = new ClassPathResource("wechat/apiclient_cert.p12");
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(resource.getInputStream(), keyPwd.toCharArray());
        X509Certificate certificate = (X509Certificate) keyStore.getCertificate("Tenpay Certificate");
        PublicKey publicKey = certificate.getPublicKey();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey("Tenpay Certificate", keyPwd.toCharArray());
        System.out.println(RsaUtil.getPublicKeyStr(publicKey));
        System.out.println("============");
        System.out.println(RsaUtil.getPrivateKeyStr(privateKey));

        ClassPathResource resource1 = new ClassPathResource("wechat/apiclient_key.pem");
        PrivateKey privateKey1 = PemUtil.loadPrivateKey(resource1.getInputStream());
        System.out.println("ddddd");
    }

}
